using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Portal.Core.DAL;
using System.ComponentModel;

namespace Portal.BO.Editoral.LogNews
{
    public class LogHelper
    {        
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Sort"></param>
        /// <param name="StartIndex"></param>
        /// <param name="Length"></param>
        /// <returns></returns>
        /// 
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataView SelectAll(string Sort, int StartRow, int PageSize)
        {
            if (Sort == null) { Sort = String.Empty; }
            using (MainDB db = new MainDB())
            {
                //DataTable table = db.StoredProcedures.vc_Log_SelectAll(StartRow, PageSize);
                DataTable table = db.StoredProcedures.vc_Log_SelectAll(Sort, StartRow, PageSize);
                DataView view = table.DefaultView;              
                return view;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="log_news"></param>
        public void UpdateNewsPublished(Log_News log_news)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.vc_NewsPublished_Update(log_news);                
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Commment_ID"></param>
        /// <returns></returns>
        public DataTable SelectCommentByCommentID(Int64 Commment_ID)
        {
            using (MainDB db = new MainDB())
            {
                DataTable table = db.StoredProcedures.vc_SelectCommentByID(Commment_ID);
                return table;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="log_news"></param>
        public void UpdateNews(Log_News log_news)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.vc_News_Update(log_news);
            }            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="log_news_id"></param>
        /// <returns></returns>
        public DataTable SelectOneLogNews(Int64 log_news_id)
        {
            using (MainDB db = new MainDB())
            {
                DataTable table = db.StoredProcedures.vc_SelectOneLogNews(log_news_id);
                return table;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="log_news_id"></param>
        /// <returns></returns>
        public Log_News SelectOneLogNewsObject(Int64 log_news_id)
        {
            using (MainDB db = new MainDB())
            {
                DataTable table = db.StoredProcedures.vc_SelectOneLogNews(log_news_id);
                Log_News logNews = new Log_News();
                if (table.Rows.Count != 0)
                {
                    logNews.Cat_ID = Convert.ToInt32(table.Rows[0]["Cat_ID"]);
                    logNews.IsComment = Convert.ToBoolean(table.Rows[0]["isComment"]);
                    logNews.IsUserRate = Convert.ToBoolean(table.Rows[0]["isUserRate"]);
                    if (!table.Rows[0]["Log_CreatedDate"].ToString().Equals(String.Empty))
                    {
                        logNews.Log_CreatedDate = Convert.ToDateTime(table.Rows[0]["Log_CreatedDate"]);
                    }
                    //logNews.Log_CreatedDate = Convert.ToDateTime(table.Rows[0]["Log_CreatedDate"]);
                    logNews.Log_News_ID = Convert.ToInt32(table.Rows[0]["Log_News_ID"]);
                    logNews.News_Author = table.Rows[0]["News_Author"].ToString();
                    logNews.News_Content = table.Rows[0]["News_Content"].ToString();
                    if (!table.Rows[0]["News_CreateDate"].ToString().Equals(String.Empty))
                    {
                        logNews.News_CreateDate = Convert.ToDateTime(table.Rows[0]["News_CreateDate"]);
                    }
                    //logNews.News_CreateDate = Convert.ToDateTime(table.Rows[0]["News_CreateDate"]);
                    logNews.News_CurrEditor = table.Rows[0]["News_CurrEditor"].ToString();
                    logNews.News_Image = table.Rows[0]["News_Image"].ToString();
                    logNews.News_ImageNote = table.Rows[0]["News_ImageNote"].ToString();
                    logNews.News_isFocus = Convert.ToBoolean(table.Rows[0]["News_isFocus"]);
                    logNews.News_Mode = Convert.ToInt32(table.Rows[0]["News_Mode"]);

                    if (!table.Rows[0]["News_ModifiedDate"].ToString().Equals(String.Empty))
                    {
                        logNews.News_ModifiedDate = Convert.ToDateTime(table.Rows[0]["News_ModifiedDate"]);
                    }
                    //logNews.News_ModifiedDate = Convert.ToDateTime(table.Rows[0]["News_ModifiedDate"]);
                    logNews.News_Other = table.Rows[0]["News_OtherCat"].ToString();

                    if (!table.Rows[0]["News_PublishDate"].ToString().Equals(String.Empty))
                    {
                        logNews.News_PublishDate = Convert.ToDateTime(table.Rows[0]["News_PublishDate"]);
                    }
                    //logNews.News_PublishDate = Convert.ToDateTime(table.Rows[0]["News_PublishDate"]);
                    if (!table.Rows[0]["News_Rate"].ToString().Equals(String.Empty))
                    {
                        logNews.News_Rate = Convert.ToDecimal(table.Rows[0]["News_Rate"]);
                    }
                    logNews.News_Relation = table.Rows[0]["News_Relation"].ToString();
                    logNews.News_Status = Convert.ToInt32(table.Rows[0]["News_Status"]);
                    logNews.News_Subtitle = table.Rows[0]["News_Subtitle"].ToString();
                    if (!table.Rows[0]["News_SwitchTime"].ToString().Equals(String.Empty))
                    {
                        logNews.News_SwitchTime = Convert.ToDateTime(table.Rows[0]["News_SwitchTime"]);
                    }
                    logNews.News_Title = table.Rows[0]["News_Title"].ToString();

                    if (!table.Rows[0]["News_ViewNum"].ToString().Equals(String.Empty))
                    {
                        logNews.News_ViewNum = Convert.ToInt32(table.Rows[0]["News_ViewNum"].ToString());
                    }
                    logNews.Template = Convert.ToInt32(table.Rows[0]["Template"].ToString());
                    logNews.News_ID = Convert.ToInt64(table.Rows[0]["News_ID"].ToString());
                }
                return logNews;
            }            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="News_ID"></param>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataView SelectAllLogNewsByNewsId(Int64 News_ID)
        {
            using (MainDB db = new MainDB())
            {
                DataTable table = db.StoredProcedures.vc_LogNewsSelectAllByNewsID(News_ID);
                DataView view = table.DefaultView;
                return view;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="numPage"></param>
        /// <returns></returns>
        [DataObjectMethod(DataObjectMethodType.Select)]
        public DataTable GetPage(int numPage)
        {
            int intPagenum = numPage;
            DataTable objTb = new DataTable();
            objTb.Columns.Add(new DataColumn("Text", typeof(string)));
            objTb.Columns.Add(new DataColumn("Value", typeof(string)));
            for (int i = 1; i <= intPagenum; i++)
            {
                DataRow myRow = objTb.NewRow();
                myRow["Text"] = i.ToString();
                myRow["Value"] = Convert.ToString(i - 1);
                objTb.Rows.Add(myRow);
            }
            if (intPagenum == 0)
            {
                DataRow myRow = objTb.NewRow();
                myRow["Text"] = "1";
                myRow["Value"] = "0";
                objTb.Rows.Add(myRow);
            }
            return objTb;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="ThreadDetail_ID"></param>
        /// <returns></returns>
        public DataTable GetNewsTitleThreadTitle(Int32 ThreadDetail_ID)
        {
            using (MainDB db = new MainDB())
            {
                return db.StoredProcedures.vc_SelectNewsTitle_ThreadTitle(ThreadDetail_ID);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Thread_ID"></param>
        /// <returns></returns>
        public String GetTitleNewsThread(Int32 Thread_ID)
        {
            using (MainDB db = new MainDB())
            {
                return db.StoredProcedures.vc_SelectTitleNewsThread(Thread_ID);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="News_ID"></param>
        /// <returns></returns>
        public String GetNewsTitle(Int64 News_ID)
        {
            using (MainDB db = new MainDB())
            {
                return db.StoredProcedures.vc_News_SelectNewsTitleByNewsID(News_ID);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Sort"></param>
        /// <param name="StartRow"></param>
        /// <param name="PageSize"></param>
        /// <returns></returns>
        public int GetNumRows(string Sort, int StartRow, int PageSize)
        {
            if (Sort == null) { Sort = String.Empty; }
            using (MainDB db = new MainDB())
            {
                return db.StoredProcedures.vc_Log_SelectTotalRows(Sort);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logRow"></param>
        public void Insert(LogRow logRow)
        {
            String News_Title = String.Empty;
            using (MainDB db = new MainDB())
            {
                if (logRow.News_ID != 0)
                {
                    logRow.Object_ID = db.StoredProcedures.vc_Log_News_SelectLogNewsID(logRow.News_ID);
                    News_Title = db.StoredProcedures.vc_News_SelectNewsTitleByNewsID(logRow.News_ID);
                    logRow.Action = String.Format(logRow.Action, News_Title);
                }
                else
                {
                    logRow.Object_ID = 0;
                }
                db.StoredProcedures.vc_Log_Insert(logRow.UserName, logRow.CreatedDate, logRow.Action, logRow.Type, logRow.Object_ID);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logRow"></param>
        public void InsertFlowEvent(LogRow logRow)
        {
            using (MainDB db = new MainDB())
            {
                if (logRow.News_ID != 0)
                {
                    logRow.Object_ID = 0;
                    //logRow.Action = String.Format(logRow.Action, String.Empty);
                }
                else
                {
                    logRow.Object_ID = 0;
                }
                db.StoredProcedures.vc_Log_Insert(logRow.UserName, logRow.CreatedDate, logRow.Action, logRow.Type, logRow.Object_ID);
            }            
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="logRow"></param>
        public void Update(LogRow logRow)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.vc_Log_Update(logRow.UserName, logRow.CreatedDate, logRow.Action, logRow.Type,logRow.Object_ID, logRow.Log_ID);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="LogID"></param>
        /// <returns></returns>
        public DataTable SelectOne(Int64 LogID)
        {             
            using (MainDB db = new MainDB())
            {
                DataTable table = db.StoredProcedures.vc_Log_SelectOne(LogID);
                return table;
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="LogID"></param>
        public void Delete(Int64 LogID)
        {
            using (MainDB db = new MainDB())
            {
                db.StoredProcedures.vc_Log_Delete(LogID);
            }
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Vote_ID"></param>
        /// <returns></returns>
        public String GetVoteTitle(Int32 Vote_ID)
        {
            using (MainDB db = new MainDB())
            {
                DataTable table = db.StoredProcedures.vc_SelectVoteByID(Vote_ID);
                if (table.Rows.Count == 0)
                {
                    return String.Empty;
                }
                else
                {
                    return table.Rows[0]["Vote_Title"].ToString();
                }
            }
        }
    }
}
